package com.wuye.charge.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.wuye.charge.domain.vo.WyBalconyunithomeVo;
import com.wuye.charge.domain.vo.WyCommunityVo;
import com.wuye.charge.service.WyBalconyunithomeVoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wuye.common.annotation.Log;
import com.wuye.common.core.controller.BaseController;
import com.wuye.common.core.domain.AjaxResult;
import com.wuye.common.enums.BusinessType;
import com.wuye.charge.domain.WyCostAllocation;
import com.wuye.charge.service.IWyCostAllocationService;
import com.wuye.common.utils.poi.ExcelUtil;
import com.wuye.common.core.page.TableDataInfo;

/**
 * 费用分配Controller
 *
 * @author lishuo
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/charge/allocation")
public class WyCostAllocationController extends BaseController {
    @Autowired
    private IWyCostAllocationService wyCostAllocationService;
    @Autowired
    private WyBalconyunithomeVoService wyBalconyunithomeVoService;

    /**
     * 查询费用分配列表
     */
    @PreAuthorize("@ss.hasPermi('charge:allocation:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestParam(value = "pageNum") int pageNum,
                              @RequestParam(value = "pageSize") int pageSize,
            @RequestBody WyCostAllocation wyCostAllocation
                                ) {
        PageHelper.startPage(pageNum, pageSize);
        List<WyCostAllocation> list = wyCostAllocationService.selectWyCostAllocationList(wyCostAllocation);
        return getDataTable(list);
    }

    /**
     * 导出费用分配列表
     */
    @PreAuthorize("@ss.hasPermi('charge:allocation:export')")
    @Log(title = "费用分配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyCostAllocation wyCostAllocation) {
        List<WyCostAllocation> list = wyCostAllocationService.selectWyCostAllocationList(wyCostAllocation);
        ExcelUtil<WyCostAllocation> util = new ExcelUtil<WyCostAllocation>(WyCostAllocation.class);
        util.exportExcel(response, list, "费用分配数据");
    }

    /**
     * 获取费用分配详细信息
     */
    @PreAuthorize("@ss.hasPermi('charge:allocation:query')")
    @GetMapping(value = "/{allocationId}")
    public AjaxResult getInfo(@PathVariable("allocationId") Long allocationId) {
        return success(wyCostAllocationService.selectWyCostAllocationByAllocationId(allocationId));
    }

    /*
     *查询
     * @author ls
     * @date 2024/11/23 14:00
     */
    @PreAuthorize("@ss.hasPermi('charge:allocation:query')")
    @GetMapping("/findCommunity")
    public AjaxResult findCommunity(WyCommunityVo wyCommunityVo) {
        List<WyCommunityVo> wyCommunityVos = wyBalconyunithomeVoService.selectWyCommunityVoList(wyCommunityVo);
        return success(wyCommunityVos);
    }


    @PreAuthorize("@ss.hasPermi('charge:allocation:query')")
    @GetMapping("/findBalconyUnitHome")
    public AjaxResult findBalconyUnitHome(WyBalconyunithomeVo wyBalconyunithomeVo) {
        List<WyBalconyunithomeVo> wyBalconyunithomeVos = wyBalconyunithomeVoService.selectWyBalconyunithomeVoList(wyBalconyunithomeVo);
        Map<Object, Object> balconyMap = new HashMap<>();
        Map<Object, Object> unitMap = new HashMap<>();
        Map<Object, Object> homeMap = new HashMap<>();
        List idList = new ArrayList();
        for (WyBalconyunithomeVo item : wyBalconyunithomeVos) {
            balconyMap.put(item.getBalconyId(), item.getBalconyName());
            unitMap.put(item.getUnitId(), item.getUnitName());
            homeMap.put(item.getHomeId(), item.getHomeNumber());
            if (item.getId() != null)
                idList.add(item.getId());
        }
        List balconyList = new ArrayList();
        for (Map.Entry<Object, Object> entry : balconyMap.entrySet()) {
            Map map = new HashMap();
            map.put("balconyId", entry.getKey());
            map.put("balconyName", entry.getValue());
            balconyList.add(map);
        }
        List unitList = new ArrayList();
        for (Map.Entry<Object, Object> entry : unitMap.entrySet()) {
            Map map = new HashMap();
            map.put("unitId", entry.getKey());
            map.put("unitName", entry.getValue());
            unitList.add(map);
        }
        List homeList = new ArrayList();
        for (Map.Entry<Object, Object> entry : homeMap.entrySet()) {
            if (entry.getKey()!=null){
                Map map = new HashMap();
                map.put("homeId", entry.getKey());
                map.put("homeNumber", entry.getValue());
                homeList.add(map);
            }

        }
        List resultList = new ArrayList();
        resultList.add(balconyList);
        resultList.add(unitList);
        resultList.add(homeList);
        if (idList != null && !idList.isEmpty()) {
            resultList.add(idList);
        }
        return success(resultList);
    }

    /**
     * 新增费用分配
     */
    @PreAuthorize("@ss.hasPermi('charge:allocation:add')")
    @Log(title = "费用分配", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyCostAllocation wyCostAllocation) {
        return toAjax(wyCostAllocationService.insertWyCostAllocation(wyCostAllocation));
    }

    /**
     * 修改费用分配
     */
    @PreAuthorize("@ss.hasPermi('charge:allocation:edit')")
    @Log(title = "费用分配", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyCostAllocation wyCostAllocation) {
        return toAjax(wyCostAllocationService.updateWyCostAllocation(wyCostAllocation));
    }

    /**
     * 删除费用分配
     */
    @PreAuthorize("@ss.hasPermi('charge:allocation:remove')")
    @Log(title = "费用分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/{allocationIds}")
    public AjaxResult remove(@PathVariable Long[] allocationIds) {
        return toAjax(wyCostAllocationService.deleteWyCostAllocationByAllocationIds(allocationIds));
    }
}
